<template>
<div v-show="this.$store.state.isShowAddStaff" class="js_dialog" id="addDialog">
    <div class="weui-mask"></div>
    <div class="weui-dialog">
        <div class="weui-dialog__hd"><strong class="weui-dialog__title">添加营业员</strong></div>
        <div class="weui-dialog__bd">
            <div class="weui-cells">
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset"><input v-model="staffName" class="weui-input" type="text" placeholder="请输入营业员姓名" datatype="s2-8" nullmsg="请输入名字！" errormsg="名字在2-8个汉字"></div>
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset"><input  v-model="staffPhone" id="staff_phone" class="weui-input" type="tel" placeholder="请输入营业员手机号"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="weui-dialog__ft">
            <a @click="closeAddStaffDialog" href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default ui-link">取消</a>
            <a @click="addStaff" href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary ui-link">确定</a>
        </div>
    </div>
</div>
</template>

<script>
export default  {
    name: 'addStaff',  // 组件名称
    data () {  // 数据
        return {
            staffName:"",
            staffPhone:"",
        }
    },
    methods:{ // 自定义方法，可对data进行处理
        closeAddStaffDialog(){
            this.$store.state.isShowAddStaff = false;
        },
        addStaff(){  // 增加营业员
            this.$store.state.isShowAddStaff = false;
            this.$store.state.showLoading = true;
            this.postAddStaff();
        },
        // 请求增加收银员
        postAddStaff() {
            var asParameters = {
                "token": this.$store.state.token,
                "bizLicNo": this.$store.state.loginName,
                "loginName": this.$store.state.loginName,
                "openid": this.$store.state.openid,
                "loginType": this.$store.state.loginType,
                "cust_name": this.staffName,
                "phoneNo": this.staffPhone
            }
            var _this = this;
            $.post(AddStaff_URL, asParameters, function(asdata) {
                _this.$store.state.showLoading = false;
                _this.addStaffBack(asdata);
            })
        },

        // 增加收银员回调函数
        addStaffBack(asdata) {
            this.$store.state.showLoading = false;
            console.dir(asdata);
           /* if (asdata.login_response) {
                haslogin(asdata);
                return;
            }*/
            if (asdata.cashier_response && asdata.cashier_response.resp_code == "0000") {
                this.$store.state.errorMsg = asdata.cashier_response.resp_msg;// 添加成功提示
                this.sendMessage(1);
                this.getStaffs();
                location.hash = "#/userSetting";
            } else if (asdata.cashier_response && asdata.cashier_response.resp_msg) {
                this.$store.state.errorMsg = asdata.cashier_response.resp_msg;
            } else if (asdata.error_response) {
                var errorMsg = asdata.error_response.msg ? asdata.error_response.msg : "添加失败";
                this.$store.state.errorMsg = errorMsg;
            }
        },
        // 商户增加删除营业员给营业员发短信
        sendMessage(delAdd) {
            var corp_abbr = sessionStorage.getItem("corp_abbr");
            var parameters = {
                "openid": this.$store.state.openid,
                "lodinId": this.staffPhone,
                "position": this.$store.state.loginType,
                "merName": corp_abbr,
                "sendType": "4"
            };
            if (delAdd == 0) { // 删除
                parameters.sendType = "5";
            }

            $.post(duanxin_URL, parameters, function(dxdata) {
                console.dir(dxdata);
            });
        },
        getStaffs(){ // 取营业员列表
            var parameter = {
                token:this.$store.state.token,
                loginName:this.$store.state.loginName,
                bizLicNo:this.$store.state.loginName,
                openid:this.$store.state.openid,
                loginType:1
            };
            var _this = this;
            $.post(queryStaff_URL,parameter,function(staffsData){
                console.dir(staffsData);
                if(staffsData.cashier_response && staffsData.cashier_response.cashier_info){
                    var infos = staffsData.cashier_response.cashier_info;
                    if (!infos.length) { // 返回一个营业员时不是数组处理
                        var ninfos = [];
                        ninfos.push(infos);
                        infos = ninfos;
                    }
                    _this.$store.state.staffs = infos;
                    }
                });

            },
    },
    beforeMount:function () {
        this.staffPhone = "";
    }
}
</script>

<style lang='scss' scoped>

</style>
